/*
** Created by Gahon Alexis
*/

#include "Menu.hh"

Menu::Menu(Window &win) : GameScreen(win), _Window(win)
{
    this->_Font = new sf::Font();
    this->_Font->LoadFromFile("starcraft.ttf");

    this->_Content = new RessourceManager();
    this->_Content->AddImage("bg-space.jpg");
    this->_Background = new Background(this->_Window);
    const sf::Image &_image2 = this->_Content->GetImage("bg-space.jpg");
    sf::Sprite  *_sprite2 = new sf::Sprite();
    _sprite2->SetImage(_image2);
    this->_Background->Initialize(*_sprite2, *new sf::Vector2f(-0.5, 0));

    sf::String  *Play = new sf::String("Jouer", *this->_Font, 40);
    Play->SetStyle(sf::String::Underlined);
    Play->SetColor(sf::Color(200, 128, 0));
    Play->SetPosition(this->_Window.GetWidth()/3.0, 50.0f);
    this->_Items.push_back(Play);

    sf::String  *Setting = new sf::String("Option", *this->_Font, 40);
    Setting->SetPosition(this->_Window.GetWidth()/3.0, 150.0f);
    this->_Items.push_back(Setting);

    sf::String  *Exit = new sf::String("Quitter", *this->_Font, 40);
    Exit->SetPosition(this->_Window.GetWidth()/3.0, 250.0f);
    this->_Items.push_back(Exit);

    this->_Current = this->_Items.begin();

    this->_ItemSelect.insert(std::pair<std::string, GameState>("Jouer", GAME));
    this->_ItemSelect.insert(std::pair<std::string, GameState>("Option", MENU_OPTIONS));
    this->_ItemSelect.insert(std::pair<std::string, GameState>("Quitter", EXIT));
}

Menu::~Menu()
{
}

int     Menu::Run(void)
{
    bool    isRunning = true;
    GameTime    *gameTime = new GameTime();
    Time        timer = 0.0f;
    sf::String  *item;

    while (isRunning)
    {
        timer += gameTime->ElapsedGameTime();
        sf::Event e;
        while (this->_Window.GetEvent(e))
            if ((e.Type == sf::Event::Closed))
                isRunning = false;
            else if ((e.Type == sf::Event::KeyPressed) && (e.Key.Code == sf::Key::Escape))
                isRunning = false;
            else if ((e.Type == sf::Event::KeyPressed) && (e.Key.Code == sf::Key::Up))
                this->GoPrev();
            else if ((e.Type == sf::Event::KeyPressed) && (e.Key.Code == sf::Key::Down))
                this->GoNext();
            else if ((e.Type == sf::Event::KeyPressed) && (e.Key.Code == sf::Key::Return))
                {
                    item = *this->_Current;
                    std::string t = item->GetText();
                    for (std::map<std::string, GameState>::iterator it = _ItemSelect.begin(); it != _ItemSelect.end(); ++it)
                    {
                        if ((*it).first == t){
                            std::cout << t << std::endl;
                            return (*it).second;
                        }
                    }
                }
        this->Update(gameTime->ElapsedGameTime());
        this->Draw(gameTime->ElapsedGameTime());
    }
    return EXIT;
}

void    Menu::Update(Time ElapsedTime)
{
}

void    Menu::Draw(Time ElapsedTime)
{
    std::list<sf::String*>::iterator it;

    this->_Window.Clear();
    this->_Background->Draw(ElapsedTime);
    for (it = this->_Items.begin(); it != this->_Items.end(); ++it){
        this->_Window.Draw(*(*it));
    }
    this->_Window.Display();
}

void    Menu::GoNext(void)
{
    sf::String  *item;
    std::list<sf::String*>::iterator    tmp;

    tmp = this->_Current;
    ++tmp;
    if (tmp == this->_Items.end())
    {
        item = *this->_Current;
        item->SetColor(sf::Color(255, 255, 255));
        item->SetStyle(sf::String::Regular);
        this->_Current = this->_Items.begin();
    }
    else
    {
        item = *this->_Current;
        item->SetColor(sf::Color(255, 255, 255));
        item->SetStyle(sf::String::Regular);
        if (this->_Current != this->_Items.end())
            ++this->_Current;
    }

    item = *this->_Current;
    item->SetColor(sf::Color(200, 128, 0));
    item->SetStyle(sf::String::Underlined);
}

void    Menu::GoPrev(void)
{
    sf::String  *item;
    if (this->_Current == this->_Items.begin())
    {
        item = *this->_Current;
        item->SetColor(sf::Color(255, 255, 255));
        item->SetStyle(sf::String::Regular);
        this->_Current = this->_Items.end();
        --this->_Current;
    }
    else
    {
        item = *this->_Current;
        item->SetColor(sf::Color(255, 255, 255));
        item->SetStyle(sf::String::Regular);
        --this->_Current;
    }
    item = *this->_Current;
    item->SetColor(sf::Color(200, 128, 0));
    item->SetStyle(sf::String::Underlined);
}

int     Menu::Enter(void)
{
    return GAME;
}
